package extration;

import java.util.ArrayList;
import java.util.HashMap;

public class Mot {
	private String chaine;
	//L'occurence general du mot dans toutes les polarites
	private int nbOccGenerale;
	//Le nombre de messages dans lequel se trouve le mot dans toutes les polarites
	private int nbMessage;
	//occurence pour chaque polarite
	private HashMap<String,Integer> nbOccPolarite;
	//Nb de message dans lequel le mot apparait por chaque polarite
	private HashMap<String,Integer> nbMsgPolarite;
	//Khi pour chaque polarite
	private HashMap<String,Double> khiPolarite;
	//Tableau contenant le tableau d'effectif observe par polarite
	private HashMap<String,ArrayList<Double>> tabEffectif;
	// Tableau effectif THEORIQUE par polarite
	private HashMap<String,ArrayList<Double>> tabEffectifTh ;
	
	public Mot(String c){
		this.chaine = c;
		this.nbOccGenerale = 0;
		this.nbMessage = 0;
		this.nbOccPolarite = new HashMap<String,Integer>();
		this.nbOccPolarite.put(Data.POSITIVE, 0);
		this.nbOccPolarite.put(Data.NEGATIVE, 0);
		this.nbOccPolarite.put(Data.NEUTRAL, 0);
		this.nbOccPolarite.put(Data.IRRELEVANT, 0);
		this.khiPolarite = new HashMap<String,Double>();
		this.khiPolarite.put(Data.POSITIVE, 0.0);
		this.khiPolarite.put(Data.NEGATIVE, 0.0);
		this.khiPolarite.put(Data.NEUTRAL, 0.0);
		this.khiPolarite.put(Data.IRRELEVANT, 0.0);
		this.tabEffectif = new HashMap<String,ArrayList<Double>>();
		this.tabEffectif.put(Data.POSITIVE, new ArrayList<Double>());
		this.tabEffectif.put(Data.NEGATIVE, new ArrayList<Double>());
		this.tabEffectif.put(Data.NEUTRAL, new ArrayList<Double>());
		this.tabEffectif.put(Data.IRRELEVANT, new ArrayList<Double>());
		this.tabEffectifTh = new HashMap<String,ArrayList<Double>>();
		this.tabEffectifTh.put(Data.POSITIVE, new ArrayList<Double>());
		this.tabEffectifTh.put(Data.NEGATIVE, new ArrayList<Double>());
		this.tabEffectifTh.put(Data.NEUTRAL, new ArrayList<Double>());
		this.tabEffectifTh.put(Data.IRRELEVANT, new ArrayList<Double>());
		this.nbMsgPolarite = new HashMap<String,Integer>();
		this.nbMsgPolarite.put(Data.POSITIVE, 0);
		this.nbMsgPolarite.put(Data.NEGATIVE, 0);
		this.nbMsgPolarite.put(Data.NEUTRAL, 0);
		this.nbMsgPolarite.put(Data.IRRELEVANT, 0);
	}
	
	//adder
	public void addOccPolarite(String polarite){
			int nb = this.nbOccPolarite.get(polarite);
			this.nbOccPolarite.put(polarite,nb+1);
	}
	public void addNbMsgPolarite(String polarite){
			int nb = this.nbMsgPolarite.get(polarite);
			this.nbMsgPolarite.put(polarite,nb+1);
	}
	
	public void addTabEffectif(String p, double a, double b, double c, double d){
		ArrayList<Double> array = this.tabEffectif.get(p);
		array.add(a);
		array.add(b);
		array.add(c);
		array.add(d);
		this.tabEffectif.put(p, array);
	}
	
	//Calcul du tableau effectif
	public void calculTabEffectifTh(String p,int N){
		ArrayList<Double> array = this.tabEffectif.get(p);
		double L1 = array.get(0) + array.get(1);
		double L2 = array.get(2) + array.get(3);
		double C1 = array.get(0) + array.get(2);
		double C2 = array.get(1) + array.get(3);
		
		ArrayList<Double> array2 = this.tabEffectifTh.get(p);
		
		array2.add((C1*L1)/N);
		array2.add((C2*L1)/N);
		array2.add((C1*L2)/N);
		array2.add((C2*L2)/N);
		
		this.tabEffectifTh.put(p, array2);
		this.calculKhi(p);
	}
	
	//calcul du khi
	public void calculKhi(String polarite){
		ArrayList<Double> array = this.tabEffectif.get(polarite);
		double a =array.get(0);
		double b =array.get(1);
		double c =array.get(2);
		double d =array.get(3);
		
		ArrayList<Double> array2 = this.tabEffectifTh.get(polarite);
		double A =array2.get(0);
		double B =array2.get(1);
		double C =array2.get(2);
		double D =array2.get(3);
		
		double res = (Math.pow((a-A),2)/A) + (Math.pow((b-B),2)/B) + (Math.pow((c-C),2)/C) + (Math.pow((d-D),2)/D)  ;
		
		this.khiPolarite.put(polarite, res);
	}
		
	//getter setter
	public String getChaine() {
		return chaine;
	}
	public int getNbOccGenerale() {
		return nbOccGenerale;
	}
	public int getNbMessage(){
		return nbMessage;
	}
	public void addOccGenerale(){
		this.nbOccGenerale++;
	}
	public void addNbMessage(){
		this.nbMessage++;
	}
	public HashMap<String,Double> getKhiPolarite(){
		return this.khiPolarite;
	}
	public HashMap<String,Integer> getNbMsgPolarite(){
		return this.nbMsgPolarite;
	}
	public HashMap<String,Integer> getNbOccPolarite(){
		return this.nbOccPolarite;
	}
	public HashMap<String,ArrayList<Double>> getTabEffectif(){
		return this.tabEffectif;
	}
	public HashMap<String,ArrayList<Double>> getTabEffectifTh(){
		return this.tabEffectifTh;
	}
	public ArrayList<Double> getTabEffectif(String pol){
		if(pol.equals(Data.NEUTRAL))
			return this.tabEffectif.get(Data.NEUTRAL);
		if(pol.equals(Data.NEGATIVE))
			return this.tabEffectif.get(Data.NEGATIVE);
		if(pol.equals(Data.IRRELEVANT))
			return this.tabEffectif.get(Data.IRRELEVANT);
		
		return this.tabEffectif.get(Data.POSITIVE);
	}
	public ArrayList<Double> getTabEffectifTh(String pol){
		if(pol.equals(Data.NEUTRAL))
			return this.tabEffectifTh.get(Data.NEUTRAL);
		if(pol.equals(Data.NEGATIVE))
			return this.tabEffectifTh.get(Data.NEGATIVE);
		if(pol.equals(Data.IRRELEVANT))
			return this.tabEffectifTh.get(Data.IRRELEVANT);
		
		return this.tabEffectifTh.get(Data.POSITIVE);
	}
	//getter special
	public int getOccPolarite(String pol){
		if(pol.equals(Data.NEUTRAL))
			return this.nbOccPolarite.get(Data.NEUTRAL);
		if(pol.equals(Data.NEGATIVE))
			return this.nbOccPolarite.get(Data.NEGATIVE);
		if(pol.equals(Data.IRRELEVANT))
			return this.nbOccPolarite.get(Data.IRRELEVANT);
		
		return this.nbOccPolarite.get(Data.POSITIVE);	
	}
	public int getNbMsgPolarite(String pol){
		if(pol.equals(Data.NEUTRAL))
			return this.nbMsgPolarite.get(Data.NEUTRAL);
		if(pol.equals(Data.NEGATIVE))
			return this.nbMsgPolarite.get(Data.NEGATIVE);
		if(pol.equals(Data.IRRELEVANT))
			return this.nbMsgPolarite.get(Data.IRRELEVANT);
		
		return this.nbMsgPolarite.get(Data.POSITIVE);	
	}
	public int getNbMsgNonPolarite(String pol){
		int nb=0;
		if(!pol.equals(Data.NEUTRAL))
			nb+= this.nbMsgPolarite.get(Data.NEUTRAL);
		if(!pol.equals(Data.NEGATIVE))
			nb+=this.nbMsgPolarite.get(Data.NEGATIVE);
		if(!pol.equals(Data.IRRELEVANT))
			nb+= this.nbMsgPolarite.get(Data.IRRELEVANT);
		if(!pol.equals(Data.POSITIVE))
			nb+=this.nbMsgPolarite.get(Data.POSITIVE);
		return nb;
	}
	
	public double getKhiPolarite(String pol){
		if(pol.equals(Data.NEUTRAL))
			return this.khiPolarite.get(Data.NEUTRAL);
		if(pol.equals(Data.NEGATIVE))
			return this.khiPolarite.get(Data.NEGATIVE);
		if(pol.equals(Data.IRRELEVANT))
			return this.khiPolarite.get(Data.IRRELEVANT);
		
		return this.khiPolarite.get(Data.POSITIVE);	
	}
	
	
}
